//
//  ContentViewController.m
//  Runone
//
//  Created by runone01 on 16/10/22.
//  Copyright © 2016年 runone. All rights reserved.
//

#import "ContentViewController.h"
#import "ContentCell.h"
#import "DMCMSCurrentDataInfo.h"
#import "MJExtension.h"

static CGFloat const margin = 10.f;

static CGFloat const contentLabelY = margin + 30.f + margin;

@interface ContentViewController ()
@property(nonatomic,strong)ContentCell *MessageCell;
@property (strong, nonatomic)DMCMSCurrentDataInfo *board;
@property(nonatomic,strong)NSMutableArray *mutableArray;
@property(nonatomic)NSInteger i;
@property(nonatomic,strong)NSMutableArray *hightArray;
@property(nonatomic)CGFloat cellHight;
@property(nonatomic,strong)NSMutableArray *mutabeTime;
@end

@implementation ContentViewController

#pragma -----------情报板列表详情

- (void)viewDidLoad {
    [super viewDidLoad];
    _hightArray=[[NSMutableArray alloc] init];
    _mutabeTime=[[NSMutableArray alloc] init];
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    self.tableView.backgroundColor=[UIColor whiteColor];
    [self.tableView registerNib:[UINib nibWithNibName:@"ContentCell" bundle:nil] forCellReuseIdentifier:@"ContentCell"];
    self.tableView.bounces                      = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    _mutableArray = [[NSMutableArray alloc] init];
    for (NSDictionary *boardDic in _messageArray) {
        _board=[DMCMSCurrentDataInfo mj_objectWithKeyValues:boardDic];
        NSString *str=_board.TEXT_INFO;
        NSString *temp=[str stringByReplacingOccurrencesOfString:@";" withString:@"  "];
        [_mutabeTime addObject:_board.LAST_UPDATE];
        [_mutableArray addObject:temp];
        
        [_hightArray addObject:str];
        NSLog(@"%lu",(unsigned long)_hightArray.count);

    }
    if (_mutableArray.count==1) {
        NSString *str=_mutableArray[0];
        NSString *time=_mutabeTime[0];
        CGFloat contentW = [UIScreen mainScreen].bounds.size.width - 2 * margin;
        CGFloat HMessage=[str boundingRectWithSize:CGSizeMake(contentW, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15.f]} context:nil].size.height;
        CGFloat HTime=[time boundingRectWithSize:CGSizeMake(contentW, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15.f]} context:nil].size.height;
        _cellHight = contentLabelY + HMessage + HTime +15 ;
        self.preferredContentSize=CGSizeMake(self.view.frame.size.width-60, _cellHight);
    }
    else
    {
        self.preferredContentSize=CGSizeMake(self.view.frame.size.width-60, 200);
    }
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _messageArray.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ContentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ContentCell" forIndexPath:indexPath];
    cell.Message.text=_mutableArray[indexPath.row];
    cell.TimeLabel.text=[NSString stringWithFormat:@"更新时间:%@",_mutabeTime[indexPath.row]];
    if (_mutableArray.count==1) {
        cell.Message.textAlignment=NSTextAlignmentCenter;
        cell.textLabel.textAlignment=NSTextAlignmentCenter;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *str=_mutableArray[indexPath.row];
    NSString *time=_mutabeTime[indexPath.row];
    
    CGFloat contentW = [UIScreen mainScreen].bounds.size.width - 2 * margin - 60;
    CGFloat contentH = [str boundingRectWithSize:CGSizeMake(contentW, MAXFLOAT)
                                         options:NSStringDrawingUsesLineFragmentOrigin
                                      attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:13.f]}
                                         context:nil].size.height+15;
    CGFloat timeH = [time boundingRectWithSize:CGSizeMake(contentW, MAXFLOAT)
                                      options:NSStringDrawingUsesLineFragmentOrigin
                                   attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12.f]}
                                      context:nil].size.height;
    _cellHight = contentLabelY + contentH + margin -20 +timeH;
    
    return _cellHight+15;
}

/*
 // Override to support conditional editing of the table view.
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the specified item to be editable.
 return YES;
 }
 */

/*
 // Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
 if (editingStyle == UITableViewCellEditingStyleDelete) {
 // Delete the row from the data source
 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
 } else if (editingStyle == UITableViewCellEditingStyleInsert) {
 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
 }
 }
 */

/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
 }
 */

/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */

/*
 #pragma mark - Table view delegate
 
 // In a xib-based application, navigation from a table can be handled in -tableView:didSelectRowAtIndexPath:
 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
 // Navigation logic may go here, for example:
 // Create the next view controller.
 <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:<#@"Nib name"#> bundle:nil];
 
 // Pass the selected object to the new view controller.
 
 // Push the view controller.
 [self.navigationController pushViewController:detailViewController animated:YES];
 }
 */

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
